package com.ihighest.app.infox.adapter;

import io.rong.imkit.RongIM;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.LoginActivity;
import com.ihighest.app.infox.tools.SortModel;

public class SortAdapter extends BaseAdapter implements SectionIndexer {
	private List<SortModel> list = null;
	private final Context mContext;

	public SortAdapter(Context mContext, List<SortModel> list) {
		this.mContext = mContext;
		this.list = list;
	}

	/**
	 * 当ListView数据发生变化时,调用此方法来更新ListView
	 * 
	 * @param list
	 */
	public void updateListView(List<SortModel> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		ViewHolder viewHolder = null;
		final SortModel mContent = list.get(position);
		if (view == null) {
			viewHolder = new ViewHolder();
			view = LayoutInflater.from(mContext).inflate(R.layout.infox_activity_specialist_online_item, null);
			viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
			viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
			viewHolder.image = (ImageView) view.findViewById(R.id.imageView1);
			viewHolder.iv_chat = (ImageView) view.findViewById(R.id.iv_chat);
			viewHolder.iv_rankImage = (ImageView) view.findViewById(R.id.iv_rankImage);
			viewHolder.tvRank = (TextView) view.findViewById(R.id.textView1);

			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}

		// 根据position获取分类的首字母的Char ascii值
		int section = getSectionForPosition(position);

		// 如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
		if (position == getPositionForSection(section)) {
			viewHolder.tvLetter.setVisibility(View.VISIBLE);
			viewHolder.tvLetter.setText(mContent.getSortLetters());
		} else {
			viewHolder.tvLetter.setVisibility(View.GONE);
		}

		viewHolder.tvTitle.setText(this.list.get(position).getName());
		//
		// DisplayImageOptions defaultOptions = new
		// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_person_no_icon)
		// .showImageOnFail(R.drawable.infox_person_no_icon).showImageOnLoading(R.drawable.infox_person_no_icon).cacheInMemory(true)
		// .cacheOnDisc(true).build();
		// ImageLoader.getInstance().displayImage(list.get(position).getImageurl(),
		// viewHolder.image, defaultOptions);
		// ImageLoader.getInstance().displayImage(list.get(position).getRankImage(),
		// viewHolder.iv_rankImage, defaultOptions);
		AppKit.SetUserImg(list.get(position).getImageurl(), viewHolder.image);
		AppKit.SetNormalImg(list.get(position).getRankImage(), viewHolder.iv_rankImage);
		viewHolder.tvRank.setText(list.get(position).getRank());

		if (TextUtils.isEmpty(list.get(position).getUid())) {
			viewHolder.iv_chat.setVisibility(View.GONE);
		} else {
			viewHolder.iv_chat.setVisibility(View.VISIBLE);
			viewHolder.iv_chat.setTag(R.id.tag_1, list.get(position).getRid());
			viewHolder.iv_chat.setTag(R.id.tag_2, list.get(position).getUid());
			viewHolder.iv_chat.setTag(R.id.tag_3, list.get(position).getName());
			viewHolder.iv_chat.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					if (TextUtils.isEmpty(SharedPreferencesKit.getString(mContext, Const.SP_KEY_USER_ID))) {
						((Activity) mContext).startActivity(new Intent(mContext, LoginActivity.class));
						ToastKit.showShort(mContext, Const.UN_LOGIN_ALERT);
					} else {
						Const.IM_EXTRA = "speci_chat:" + ((String) arg0.getTag(R.id.tag_2)) + ":"
								+ SharedPreferencesKit.getString(mContext, Const.SP_KEY_USER_MID);
						RongIM.getInstance().startPrivateChat(mContext, ((String) arg0.getTag(R.id.tag_1)),
								((String) arg0.getTag(R.id.tag_3)));
					}
				}
			});
		}
		return view;

	}

	final static class ViewHolder {
		TextView tvLetter;
		TextView tvTitle;
		ImageView image, iv_chat, iv_rankImage;
		TextView tvRank;
	}

	/**
	 * 根据ListView的当前位置获取分类的首字母的Char ascii值
	 */
	@Override
	public int getSectionForPosition(int position) {
		return list.get(position).getSortLetters().charAt(0);
	}

	/**
	 * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
	 */
	@Override
	public int getPositionForSection(int section) {
		for (int i = 0; i < getCount(); i++) {
			String sortStr = list.get(i).getSortLetters();
			char firstChar = sortStr.toUpperCase().charAt(0);
			if (firstChar == section) {
				return i;
			}
		}

		return -1;
	}

	/**
	 * 提取英文的首字母，非英文字母用#代替。
	 * 
	 * @param str
	 * @return
	 */
	private String getAlpha(String str) {
		String sortStr = str.trim().substring(0, 1).toUpperCase();
		// 正则表达式，判断首字母是否是英文字母
		if (sortStr.matches("[A-Z]")) {
			return sortStr;
		} else {
			return "#";
		}
	}

	@Override
	public Object[] getSections() {
		return null;
	}
}